# coding:utf-8

from inspect import stack
from typing import List


class Solution:
    # 只包含(和)的字符串，找最长的串 
    def longestValidParentheses(self, s: str) -> int:
        res = 0
        SP = [-1]
        for i,ch in enumerate(s):
            if ch=='(':
                SP.append(i)
            else:
                SP.pop()
                if not SP:
                    # 此时已经是不合理的括号串了，将当前下标存进行，重开
                    SP.append(i)
                else:
                    
                    res = max(res,i-SP[-1])
        return res

    
if __name__ == '__main__':
    solu = Solution()
    print(solu.longestValidParentheses(")()())"))

